﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BL.Service;
using BL.Entity;


namespace LTO
{
    /// <summary>
    /// Interaction logic for UserSetup.xaml
    /// </summary>
    public partial class UserSetup : Window
    {
        int UserIdSelect = 0;
        tbl_UserService userService = new tbl_UserService();
        public UserSetup()
        {
            InitializeComponent();
            txtUserName.Focus();
        }



        private void LoadUser()
        {
            grdUser.ItemsSource = new tbl_UserService().GetAllUser();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            
            try
            {
                User addUser = GetUserData();
                if (addUser == null) { throw new Exception("ไม่มีข้อมูลผู้ใช้"); }
                if (addUser.UserName == string.Empty || addUser.UserName == null) { throw new Exception("กรุณาใส่ชื่อผู้ใช้"); }
                if (addUser.Password == string.Empty || addUser.Password == null) { throw new Exception("กรุณาใส่รหัสผ่าน"); }
                if (txtPassword.Password != txtRePassword.Password) { throw new Exception("รหัสผ่านไม่ตรงกัน"); }
                if (txtUserName.IsEnabled == true)
                {

                    userService.AddUser(addUser);

                }
                else
                {
                    userService.UpdateUser(addUser);
                }
                MessageBox.Show("บันทึกข้อมูลเรียนร้อย", "Message Box", MessageBoxButton.OK, MessageBoxImage.Asterisk);
                LoadUser();
                ClearUserData();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message,"",MessageBoxButton.OK,MessageBoxImage.Error);
            }

        }
        private User GetUserData()
        {
            User dto = new User();

            dto.Active = chkActive.IsChecked.Value;

            dto.UserName = txtUserName.Text;
            dto.Password = txtPassword.Password;

            dto.Name = txtName.Text;
            dto.SerName = txtSerName.Text;

            dto.Tel = txtTel.Text;
            dto.Email = txtMail.Text;
            dto.Fax = txtFax.Text;

            dto.UserID = UserIdSelect;

            dto.CreateBy = Gobalsource.UserNameLogin;
            dto.UpdateBy = Gobalsource.UserNameLogin;



            return dto;
        }

        private void grdUser_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                User select = (e.AddedItems[0] as User);
                txtFax.Text = select.Fax;
                txtMail.Text = select.Email;
                txtName.Text = select.Name;
                txtPassword.Password = BL.EncryptionService.Decode(select.Password);
                txtRePassword.Password = BL.EncryptionService.Decode(select.Password);
                txtSerName.Text = select.SerName;
                txtTel.Text = select.Tel;
                txtUserName.Text = select.UserName;
                chkActive.IsChecked = select.Active;
                txtUserName.IsEnabled = false;
                UserIdSelect = select.UserID;
            }

        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            ClearUserData();
           
        }

        private void ClearUserData()
        {
            txtFax.Text = string.Empty;
            txtMail.Text = string.Empty;
            txtName.Text = string.Empty;
            txtPassword.Password = string.Empty;
            txtRePassword.Password = string.Empty;
            txtSerName.Text = string.Empty;
            txtTel.Text = string.Empty;
            txtUserName.Text = string.Empty;
            chkActive.IsChecked = false;
            txtUserName.IsEnabled = true;
            UserIdSelect = 0;
            grdUser.UnselectAllCells();
            txtUserName.Focus();
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.userService.DeleteUser(GetUserData());
                MessageBox.Show("ลบข้อมูลเรียบร้อย", "", MessageBoxButton.OK, MessageBoxImage.Information);
                ClearUserData();
                LoadUser();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message,"",MessageBoxButton.OK,MessageBoxImage.Error);
            }
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            LoadUser();
        }
    }
}
